#### TABLE H.1: META-ANALYSIS RESULTS
#### Table format of pooled effects for main outcomes

rm(list = ls())
source("./2_code/00_setup.R")

#### STUDY 1 ####

data <- fread(paste0(data_path, "data_study1.csv"), header = TRUE)
data$worked <- as.factor(data$worked)

# Exclusion outcomes
lin_entry_economic <- lm_lin(exclusion_s ~ treat_economic, covariates = ~
                               edad + worked + male, data = data[(treat=='economic' | treat=='control')])
lin_entry_humanitarian <- lm_lin(exclusion_s ~ treat_humanitarian, covariates = ~
                                   edad + worked + male, data = data[(treat=='humanitarian' | treat=='control')])

# Prejudice outcomes
lin_prejudice_economic <- lm_lin(prejudice_s ~ treat_economic, covariates = ~
                                   edad + worked + male, data = data[(treat=='economic' | treat=='control')])
lin_prejudice_humanitarian <- lm_lin(prejudice_s ~ treat_humanitarian, covariates = ~
                                       edad + worked + male, data = data[(treat=='humanitarian' | treat=='control')])


#### STUDY 2 ####

data2 <- fread(paste0(data_path, "data_study2.csv"), header = TRUE)
data2$worked <- as.factor(data2$worked)

# Exclusion outcomes
lin_entry_economic2 <- lm_lin(exclusion_s ~ treat_economic, covariates = ~
                                edad + worked + male, data = data2[(treat=='economic' | treat=='control')])
lin_entry_humanitarian2 <- lm_lin(exclusion_s ~ treat_humanitarian, covariates = ~
                                    edad + worked + male, data = data2[(treat=='humanitarian' | treat=='control')])

# Prejudice outcomes
lin_prejudice_economic2 <- lm_lin(prejudice_s ~ treat_economic, covariates = ~
                                    edad + worked + male, data = data2[(treat=='economic' | treat=='control')])
lin_prejudice_humanitarian2 <- lm_lin(prejudice_s ~ treat_humanitarian, covariates = ~
                                        edad + worked + male, data = data2[(treat=='humanitarian' | treat=='control')])


#### STUDY 3 ####

data3 <- fread(paste0(data_path, "data_study3.csv"), header = TRUE)
data3$employed <- as.factor(data3$employed)

# Exclusion outcomes
lin_entry_economic3 <- lm_lin(exclusion_s ~ treat_economic, covariates = ~
                                edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_entry_humanitarian3 <- lm_lin(exclusion_s ~ treat_humanitarian, covariates = ~
                                    edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])

# Prejudice outcomes
lin_prejudice_economic3 <- lm_lin(prejudice_s ~ treat_economic, covariates = ~
                                    edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_prejudice_humanitarian3 <- lm_lin(prejudice_s ~ treat_humanitarian, covariates = ~
                                        edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])


#### CALCULATE OBSERVATIONS ####

# Economic treatment
n_obs_economic1 <- data %>%
  filter(treat == 'economic' | treat == 'control') %>%
  filter(complete.cases(exclusion_s, prejudice_s, treat_economic, edad, worked, male)) %>%
  summarize(count = n()) %>%
  pull(count)

n_obs_economic2 <- data2 %>%
  filter(treat == 'economic' | treat == 'control') %>%
  filter(complete.cases(exclusion_s, prejudice_s, treat_economic, edad, worked, male)) %>%
  summarize(count = n()) %>%
  pull(count)

n_obs_economic3 <- data3 %>%
  filter(treat == 'economic' | treat == 'control') %>%
  filter(complete.cases(exclusion_s, prejudice_s, treat_economic, edad, employed, male)) %>%
  summarize(count = n()) %>%
  pull(count)

obs_eco <- c(n_obs_economic1, n_obs_economic2, n_obs_economic3)

types_economic <- data.frame(
  treatment = rep('economic', 3),
  Study = c('Study 1', 'Study 2', 'Study 3'),
  n_total = obs_eco
)

# Humanitarian treatment
n_obs_human1 <- data %>%
  filter(treat == 'humanitarian' | treat == 'control') %>%
  filter(complete.cases(exclusion_s, prejudice_s, treat_humanitarian, edad, worked, male)) %>%
  summarize(count = n()) %>%
  pull(count)

n_obs_human2 <- data2 %>%
  filter(treat == 'humanitarian' | treat == 'control') %>%
  filter(complete.cases(exclusion_s, prejudice_s, treat_humanitarian, edad, worked, male)) %>%
  summarize(count = n()) %>%
  pull(count)

n_obs_human3 <- data3 %>%
  filter(treat == 'humanitarian' | treat == 'control') %>%
  filter(complete.cases(exclusion_s, prejudice_s, treat_humanitarian, edad, employed, male)) %>%
  summarize(count = n()) %>%
  pull(count)

obs_hum <- c(n_obs_human1, n_obs_human2, n_obs_human3)

types_humanitarian <- data.frame(
  treatment = rep('humanitarian', 3),
  Study = c('Study 1', 'Study 2', 'Study 3'),
  n_total = obs_hum
)


#### META-ANALYSIS ####

# Helper function to extract forest data
extract_forest_data <- function(meta_obj, narrative, outcome) {
  total_obs <- sum(meta_obj$n.e)
  data.frame(
    Study = c(meta_obj$studlab, "Pooled Effect"),
    Coefficient = c(meta_obj$TE, meta_obj$TE.random),
    Obs = c(meta_obj$n.e, total_obs),
    SE = c(meta_obj$seTE, meta_obj$seTE.random),
    Narrative = narrative,
    Outcome = outcome
  )
}


### Economic treatment, exclusion outcome ###

out_economic <- rbind(
  tidy(lin_entry_economic) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error),
  tidy(lin_entry_economic2) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error),
  tidy(lin_entry_economic3) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error)
)

out_economic <- cbind(out_economic, types_economic)

m.gen_eco <- metagen(
  TE = estimate, seTE = std.error, studlab = Study,
  data = out_economic, common = FALSE, random = TRUE,
  method.tau = "REML", n.e = n_total, title = "economic_treatment_entry"
)

forest_data_eco <- extract_forest_data(m.gen_eco, "Economic", "Open Migration")


### Humanitarian treatment, exclusion outcome ###

out_humanitarian <- rbind(
  tidy(lin_entry_humanitarian) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error),
  tidy(lin_entry_humanitarian2) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error),
  tidy(lin_entry_humanitarian3) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error)
)

out_humanitarian <- cbind(out_humanitarian, types_humanitarian)

m.gen_hum <- metagen(
  TE = estimate, seTE = std.error, studlab = Study,
  data = out_humanitarian, common = FALSE, random = TRUE,
  method.tau = "REML", n.e = n_total, title = "humanitarian_treatment_entry"
)

forest_data_hum <- extract_forest_data(m.gen_hum, "Humanitarian", "Open Migration")


### Economic treatment, prejudice outcome ###

out_economic_feel <- rbind(
  tidy(lin_prejudice_economic) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error),
  tidy(lin_prejudice_economic2) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error),
  tidy(lin_prejudice_economic3) %>% filter(term=='treat_economic') %>% dplyr::select(estimate, std.error)
)

out_economic_feel <- cbind(out_economic_feel, types_economic)

m.gen_eco_feel <- metagen(
  TE = estimate, seTE = std.error, studlab = Study,
  data = out_economic_feel, common = FALSE, random = TRUE,
  method.tau = "REML", n.e = n_total, title = "economic_treatment_prejudice"
)

forest_data_eco_feel <- extract_forest_data(m.gen_eco_feel, "Economic", "Positive Affect")


### Humanitarian treatment, prejudice outcome ###

out_humanitarian_feel <- rbind(
  tidy(lin_prejudice_humanitarian) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error),
  tidy(lin_prejudice_humanitarian2) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error),
  tidy(lin_prejudice_humanitarian3) %>% filter(term=='treat_humanitarian') %>% dplyr::select(estimate, std.error)
)

out_humanitarian_feel <- cbind(out_humanitarian_feel, types_humanitarian)

m.gen_hum_feel <- metagen(
  TE = estimate, seTE = std.error, studlab = Study,
  data = out_humanitarian_feel, common = FALSE, random = TRUE,
  method.tau = "REML", n.e = n_total, title = "humanitarian_treatment_prejudice"
)

forest_data_hum_feel <- extract_forest_data(m.gen_hum_feel, "Humanitarian", "Positive Affect")


#### EXPORT TO LATEX ####

table_list <- list(forest_data_eco, forest_data_hum, forest_data_eco_feel, forest_data_hum_feel)

dig <- c(0, 0, 3, 0, 3, 0, 0)

table_meta <- xtableList(table_list, caption = "Table Results of Meta Analysis", label = "table_metas", digits = dig)

latex_output <- capture.output(print(table_meta, caption.placement = "top"))

writeLines(latex_output, paste0(tables_path, "table_H1.tex"))